package com.lottery.shadowbd.utils;

import android.content.Context;
import android.content.res.Resources;
import android.util.TypedValue;

import java.lang.reflect.Field;

/**
 * 尺寸转换工具
 *
 * Created by huangzj on 2015/12/25.
 */
public class SizeConvertUtil {


    public static float sp2Px(Context mContext, int var1) {
        return TypedValue.applyDimension(2, (float)var1, mContext.getResources().getDisplayMetrics());
    }

    public static int sp2px(Context mContext, float var1) {
        float var2 = mContext.getResources().getDisplayMetrics().scaledDensity;
        return (int)(var1 * var2 + 0.5F);
    }

    public static float px2Sp(Context mContext, int var0) {
        return TypedValue.applyDimension(0, (float)var0, mContext.getResources().getDisplayMetrics());
    }

    public static int px2dip(Context mContext, float var1) {
        float var2 = mContext.getResources().getDisplayMetrics().density;
        return (int)(var1 / var2 + 0.5F);
    }

    public static int dip2px(Context mContext, float var1) {
        float var2 = mContext.getResources().getDisplayMetrics().density;
        return (int)(var1 * var2 + 0.5F);
    }

    public static int dp2px(Context mContext, int var1) {
        return (int) TypedValue.applyDimension(1, (float)var1, mContext.getResources().getDisplayMetrics());
    }

    public static int dpToPx(Resources var0, int var1) {
        return (int) TypedValue.applyDimension(0, (float)var1, var0.getDisplayMetrics());
    }

    public static int getScreenHeight(Context mContext) {
        return mContext.getResources().getDisplayMetrics().heightPixels;
    }

    public static int getScreenWidth(Context mContext) {
        return mContext.getResources().getDisplayMetrics().widthPixels;
    }

    public static float getStatusBarHeightWithAndroid(Context mContext) {
        float result = 50.0f;
        int resourceId = mContext.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = mContext.getResources().getDimension(resourceId);
        }
        //返回值就是状态栏的高度,得到的值是像素
        return result;
    }

    public static int getStatusBarHeight(Context mContext) {
        try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object obj = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = Integer.parseInt(field.get(obj).toString());
            return mContext.getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 50;
    }

    public static float getNavigationBarHeight(Context mContext) {
        float result = 0;
        int resourceId = mContext.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = mContext.getResources().getDimension(resourceId);
        }
        //返回值就是导航栏的高度,得到的值是像素
        return result;
    }
}
